<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/27
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Owner;
use think\Db;
use think\Config;

/**
 * 回程车订单🚛
 */
class OrderReturn extends Owner
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $db = 'order_return';


    // /**
    //  * 搜索
    //  *
    //  * @param string $str 搜索内容
    //  * @return void
    //  */
    // public function test($str = 'xiao')
    // {
    //     $where['username|nickname'] = ['LIKE','%'.$str.'%'];
    //     $list = Db::name('user')
    //         ->where($where)
    //         ->select();
    //     $this->success('ok',$list);
    // }


    


    /**
     * 发货人(货主)支付未支付的订单💰
     *
     * @param string $order 订单号
     * @param string $pay_type 默认3支付类型:1=微信,2=支付宝,3=钱包
     * @return void
     */
    public function s_pay_order($order,$pay_type='3')
    {
        $data = Db::name($this->db)->where('order',$order)->find();
        if(!$data){
            $this->error('没有此订单');
        }elseif($data['spay_status']!='2'){//发货人支付押金:1=已支付,2=未支付,3=已违约
            $this->error('已支付');
        }

        // * @param string $order_id 订单id
        // * @param string $order_status 1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
        // * @param string $uid 用户id
        // * @param string $role_status 角色:1=货主,2=司机
        // * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包
        $res = $this->PayOrder($data['id'],4,$this->auth->id,'1',$pay_type);
        $this->success('pay',$res);
    }






     /**
      * 货主获取已经抢单顺风单列表
      *
      * @param integer $page 1
      * @param integer $limit 5
      * @param integer $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
      * @return void
      */
    public function get_my_order_list($page=1,$limit=5,$status=null)
    {
        $where['s_uid'] = ['=',$this->auth->id];
        if($status){
            $where['status'] = ['=',$status];
        }
        $list = Db::name($this->db)->where($where)->page($page,$limit)->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder4FieldData($val);            
                if($val['j_uid']){

                    $val['user'] = $this->GetJuserData($val['j_uid']);//Db::name('user')->where('id',$val['j_uid'])->find();
                }        
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
            }
        }
        $this->success('ok',$list);
    }




    /**
     * 获取待完成的订单列表
     *
     * @param integer $page 1
     * @param integer $limit 5
     * @return void
     */
    public function get_todo_order_list($page=1,$limit=5)
    {
       
        $where['s_uid'] = ['=',$this->auth->id];
    
        $where['status'] = ['<',6];//$status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
        $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
        
        $list = Db::name($this->db)->where($where)->page($page,$limit)->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder4FieldData($val);    
                $val['user'] = $this->GetJuserData($val['j_uid']);             
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取常发货列表
     *
     * @return void
     */
    public function get_often_list()
    {
        $list = Db::name($this->db)->where('often_status','1')->where('s_uid',$this->auth->id)->order('time desc')->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder4FieldData($val);     
                $val['user'] = $this->GetJuserData($val['j_uid']);            
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取常发货信息
     *
     * @param int $id 常发货源id
     * @return void
     */
    public function get_often_data($id)
    {
        $val = Db::name($this->db)->where('often_status','1')->where('s_uid',$this->auth->id)->where('id',$id)->find();

        $val = $this->GetOrder4FieldData($val); 
        $val['user'] = $this->GetJuserData($val['j_uid']);
        $this->success('ok',$val);
    }

    /**
     * 获取顺风车发货信息
     * 
     * @param int $id 订单id
     * @return void
     */ 
    public function get_order4_data($id)
    {
        $data = $this->GetOrder4Data($id);
        $data['user'] = $this->GetJuserData($data['j_uid']);
        // $data['user'] = Db::name('user')->where('id',$data['j_uid'])->find();
        $this->success('ok',$data);
    }


    /**
     * 抢顺风车订单
     *
     * @param int $order_id 订单id
     * @param string $pay_type 默认3支付类型:1=微信,2=支付宝,3=钱包
     * @return void
     */
    public function grab_order4($order_id,$pay_type='3')
    {
        $order = $this->GetOrder4Data($order_id);
        if(!empty($order['s_uid'])){
            $this->error('已被接单了哦');
        }
        $data['s_uid'] = $this->auth->id;
        $data['time'] = time();
        $data['status'] ='2';
        Db::name($this->db)->where('id',$order_id)->update($data);
        
        // $this->update_order4_status($order_id,'2');//状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消


        $res = $this->PayOrder($order_id,4,$this->auth->id,'1',$pay_type);
        $this->success('pay',$res);

        //推送给用户消息已被接单


        $this->success('抢单成功',$order);
    }


    /**
     * 用户更新订单4状态
     *
     * @param int $id 订单号id
     * @param int $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function update_order4_status($id,$status)
    {
        // $where['s_uid'] = $this->auth->id;
        $where['id'] = $id;

        $res = Db::name($this->db)
                ->where($where)
                ->find();
        if(!$res){
            $this->error('没有该订单');
        }
        switch ($status) {
            case '1':
                // if($res['status']!='9')
                // $this->error('当前订单待接单');
                break;

            case '2':
                // if($res['status']!='9')
                $this->error('当前已接单,不能修改装');
                break;

            case '3':
                if($res['status']!='2')
                $this->error('未接单');
                
                break;

            case '4':         
                if($res['status']!='3')      
                $this->error('司机未到达目的地');
                
                break;

            case '5':
                if($res['status']!='4')
                $this->error('货主未确认已装车');
                
                break;

            case '6':     
                if($res['status']!='5')
                $this->error('司机未确认送达');
                break;
        //1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            default:
                $this->error('暂未加入此状态');
                break;
        }
        $this->UpdateOrder4Status($id,$status);

        $this->success('修改成功');
    }

    /**
     * 修改订单状态
     *
     * @param int $id 订单id
     * @param int $status 状态
     * @return void
     */
    protected function UpdateOrder4Status($id,$status){

        //后期加入ws推送给司机
        Db::name($this->db)->where('id',$id)->update(['status'=>$status]);
    }


}
